package org.xiaojinlong.exam;

import java.util.*;

/**
 * @author Jin Long
 * 2015/9/21
 */
public class Lock {
    private static class FakeThread {
        private int threadId;
        private List<Integer> holder = new ArrayList<Integer>();
        private Integer wait = 0;
        private boolean mark = false;
    }

    private static boolean findLock(List<FakeThread> threads, Integer wait, Integer origin, List<FakeThread> threadMemo) {
        for (FakeThread ft : threads) {
            if (ft.mark)
                continue;
            if (ft.holder.contains(wait)) {
                threadMemo.add(ft);
                if (ft.wait.equals(origin))
                    return true;
                else if (ft.wait == 0)
                    return false;
                else
                    return findLock(threads, ft.wait, origin, threadMemo);
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            List<FakeThread> threads = new ArrayList<FakeThread>();
            int total = scanner.nextInt();
            scanner.nextLine();

            for (int i = 0; i < total; i++) {
                FakeThread fakeThread = new FakeThread();
                // parse input
                String[] strs = scanner.nextLine().split(" ");
                fakeThread.threadId = Integer.valueOf(strs[0]);
                if (strs.length > 1) {
                    if (strs[1].equals("")) {
                        if (strs.length > 3 && !strs[3].equals(""))
                            fakeThread.wait = Integer.valueOf(strs[3]);
                    } else {
                        for (String s : strs[1].split(",")) {
                            if (!s.equals("")) {
                                fakeThread.holder.add(Integer.valueOf(s));
                            }
                        }
                        if (strs.length > 2 && !strs[2].equals(""))
                            fakeThread.wait = Integer.valueOf(strs[2]);
                    }
                }
                threads.add(fakeThread);
            }

            // detect dead lock
            int count = 0;
            for (FakeThread ft : threads) {
                List<FakeThread> threadMemo = new ArrayList<FakeThread>();
                if (ft.mark)
                    continue;
                if (ft.wait != 0 && findLock(threads, ft.wait, ft.wait, threadMemo)) {
                        count++;
                        threadMemo.add(ft);
                        for (FakeThread aThreadMemo : threadMemo) {
                            aThreadMemo.mark = true;
                        }
                }
            }
            System.out.println(count);
        }
    }
}
